package net.siufung.market.base.tenant.controller.regulation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.tenant.mapper.ITenantMapper;
import net.siufung.market.base.tenant.mapper.ITenantTypeMapper;
import net.siufung.market.base.tenant.mapper.entity.Tenant;
import net.siufung.market.base.tenant.mapper.entity.TenantType;

import java.util.Objects;

/**
 * @author 陈建峰
 * @since 2022/6/24 3:21 下午
 */
@AllArgsConstructor
public class TenantTypeDeleteRegulation extends AbstractRegulation<Integer> {

    private static final long serialVersionUID = 5828207773203786860L;

    private final ITenantTypeMapper tenantTypeMapper;
    private final ITenantMapper tenantMapper;

    @Override
    public boolean isSatisfiedBy(Integer tenantTypeId) {
        if(Objects.isNull(tenantTypeId)){
            throw new BizServiceException("主键不能为空！");
        }
        TenantType tenantType = tenantTypeMapper.selectById(tenantTypeId);
        if(Objects.isNull(tenantType)){
            throw new BizServiceException("租户类型不存在！");
        }
        long tenantCount = tenantMapper.selectCount(
                new LambdaQueryWrapper<Tenant>().eq(Tenant::getTenantTypeId, tenantTypeId));
        if(tenantCount > 0){
            throw new BizServiceException("租户类型下存在"+tenantCount+"租户，不能删除！");
        }
        return Boolean.TRUE;
    }
}
